package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.YcInde;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.YcIndeMapper;
import com.ruoyi.system.service.IYcIndeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 油厂指标管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-15
 */
@Service
public class YcIndeServiceImpl implements IYcIndeService 
{
    @Autowired
    private YcIndeMapper ycIndeMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询油厂指标管理
     * 
     * @param indeId 油厂指标管理主键
     * @return 油厂指标管理
     */
    @Override
    public YcInde selectYcIndeByIndeId(Long indeId)
    {
        return ycIndeMapper.selectYcIndeByIndeId(indeId);
    }

    /**
     * 查询油厂指标管理列表
     * 
     * @param ycInde 油厂指标管理
     * @return 油厂指标管理
     */
    @Override
    public List<YcInde> selectYcIndeList(YcInde ycInde)
    {
        List<YcInde> indeList = ycIndeMapper.selectYcIndeList(ycInde);
        for (YcInde inde:indeList){
            if(sysDeptMapper.selectDeptById(inde.getDeptId()) != null){
                SysDept sysDept = sysDeptMapper.selectDeptById(inde.getDeptId());
                inde.setDeptIdName(sysDept.getDeptName());
            }
        }
        return indeList;
    }

    /**
     * 查询油厂指标管理列表 导出专用
     *
     * @param ycInde 油厂指标管理
     * @return 油厂指标管理集合
     */
    @Override
    public List<YcInde> selectYcIndeListUseExport(YcInde ycInde)
    {
        List<YcInde> ycIndes = ycIndeMapper.selectYcIndeList(ycInde);
        for (YcInde ycIndeUseExport : ycIndes){
            if(sysDeptMapper.selectDeptById(ycIndeUseExport.getDeptId()) != null){
                SysDept sysDept = sysDeptMapper.selectDeptById(ycIndeUseExport.getDeptId());
                ycIndeUseExport.setDeptIdName(sysDept.getDeptName());
            }
        }
        return ycIndes;
    }


    /**
     * 新增油厂指标管理
     * 
     * @param ycInde 油厂指标管理
     * @return 结果
     */
    @Override
    public int insertYcInde(YcInde ycInde)
    {
        ycInde.setCreateTime(DateUtils.getNowDate());
        return ycIndeMapper.insertYcInde(ycInde);
    }

    /**
     * 修改油厂指标管理
     * 
     * @param ycInde 油厂指标管理
     * @return 结果
     */
    @Override
    public int updateYcInde(YcInde ycInde)
    {
        return ycIndeMapper.updateYcInde(ycInde);
    }

    /**
     * 批量删除油厂指标管理
     * 
     * @param indeIds 需要删除的油厂指标管理主键
     * @return 结果
     */
    @Override
    public int deleteYcIndeByIndeIds(Long[] indeIds)
    {
        return ycIndeMapper.deleteYcIndeByIndeIds(indeIds);
    }

    /**
     * 删除油厂指标管理信息
     * 
     * @param indeId 油厂指标管理主键
     * @return 结果
     */
    @Override
    public int deleteYcIndeByIndeId(Long indeId)
    {
        return ycIndeMapper.deleteYcIndeByIndeId(indeId);
    }

    /**
     *   导入指标
     * @param indeList 指标列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    public String importData(List<YcInde> indeList, Boolean isUpdateSupport, String operName){
        if (StringUtils.isNull(indeList) || indeList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (YcInde ycInde: indeList){
            try{
                YcInde i = ycIndeMapper.selectYcIndeByIndeId(ycInde.getIndeId());
                if(StringUtils.isNull(i)){
                    BeanValidators.validateWithException(validator,ycInde);
                    //转换部门名称和部门编号
                    SysDept sysDept = sysDeptMapper.selectDeptByName(ycInde.getDeptIdName());
                    ycInde.setDeptId(sysDept.getDeptId());
                    this.insertYcInde(ycInde);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 导入成功");
                }else if (isUpdateSupport){
                    BeanValidators.validateWithException(validator,ycInde);
                    this.updateYcInde(ycInde);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 更新成功");
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + " 已存在");
                }
            } catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum +  "导入失败";
                failureMsg.append(msg + e.getMessage());

            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 查询所有指标
     *
     * @return 结果
     */
    public List<YcInde> selectYcIndeAll(){
        return ycIndeMapper.selectYcIndeAll();
    };

    /**
     * 查询指标年份
     *
     * @return 结果
     */
    public List<YcInde> selectYcIndeYear(){return ycIndeMapper.selectYcIndeYear();};

    public List<YcInde> selectYcIndeByYear(){
        return ycIndeMapper.selectYcIndeByYear();
    };

    public List<YcInde> selectYcIndeChang(){
        return ycIndeMapper.selectYcIndeChang();
    };
    public List<YcInde> selectYcIndeQu(){
        return ycIndeMapper.selectYcIndeQu();
    };
    public List<YcInde> selectYcIndeZhan(){
        return ycIndeMapper.selectYcIndeZhan();
    };

}
